package socketinterface;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ThreadFactory;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewSite;

import socketinterface.requests.ClientRequest;

import com.vast.ui.iopanel.Activator;
import com.vast.ui.iopanel.views.ViewPart;


public class CommunicationManager {
	public  final ViewPart         view;
	public  Logger                 logger;
	private Action                 startAction;
	private Action                 stopAction;
	private String                 serverName   = "localhost";
	private int                    serverPort   = 30000;
	private ServerSocket           serverSocket = null;
	public  ClientRequest          clientRequest;
	public  BlockSocketClient      client     = null;
	private boolean                isConnectingServer = false;
	
	public CommunicationManager(ViewPart view, String serverName, int serverPort) {
		this.view       = view;
		this.logger     = new ConsoleLogger();
		this.serverName = serverName;
		this.serverPort = serverPort;
		
		
		startAction = new Action() {
			public void run() {
				openConnection();
			}
		};
		startAction.setEnabled(true);
		startAction.setToolTipText("Start Socket Client");
		startAction.setImageDescriptor(Activator.getDefault().getImageRegistry().getDescriptor("START_ENABLED"));
		
		stopAction = new Action() {
			public void run() {
				closeConnection();
			}
		};
		stopAction.setToolTipText("Stop SocketServer");
		stopAction.setEnabled(false);
		stopAction.setImageDescriptor(Activator.getDefault().getImageRegistry().getDescriptor("STOP_ENABLED"));
		
		IViewSite viewSite = this.view.getViewSite();
		IActionBars actionBars = viewSite.getActionBars();
		IToolBarManager toolbar = actionBars.getToolBarManager();
		toolbar.add(startAction);
		toolbar.add(stopAction);
				
	}
	
	public void openConnection(){
		if(!isEstablished()){
			logger.writeEntry("openConnection()");
			startAction.setEnabled(false);
			stopAction.setEnabled(true);
			
			startClient();
		}
	}
	
	public void closeConnection(){
			setEstablished(false);
			startAction.setEnabled(true);
			stopAction.setEnabled(false);
	}
	
	public void setPortNumber(int portNumber) {
		this.serverPort = portNumber;
	}
	
	public synchronized void setEstablished(boolean value) {
		isConnectingServer = value;
	}
	public synchronized boolean isEstablished() {
		// TODO Auto-generated method stub
		return isConnectingServer;
	}
	
	public void startClient() {
		// TODO Auto-generated method stub
		clientRequest = new ClientRequest();
		clientRequest.openClientSocket(serverName, serverPort);
		client        = new BlockSocketClient(view, clientRequest, logger,
				new ThreadFactory(){

					public Thread newThread(Runnable r) {
						// TODO Auto-generated method stub
						return new Thread(r);
					}
		});
		setEstablished(true);
	}
	
}
